/*
 * PlaylistDownAction.java
 *
 * Created on April 2, 2006, 1:13 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.lib.actions;

import com.bff.bjj.utils.Resources;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import org.bff.slimserver.SlimPlaylist;
import org.bff.slimserver.exception.SlimException;
import org.bff.slimserver.musicobjects.SlimPlaylistItem;

/**
 *
 * @author Bill
 */
public class PlaylistDownAction extends PlaylistMoveAction {

    /** Creates a new instance of PlaylistDownAction */
    public PlaylistDownAction(JList list, SlimPlaylist playlist) {
        super(playlist, list);
      
        putValue(Action.SHORT_DESCRIPTION, Resources.getMessage(Resources.LABEL_KEY.ACTION_PLAYLIST_DOWN_SHORT_DESC));
        putValue(Action.SMALL_ICON, Resources.getIcon(Resources.ICON_KEY.ACTION_PLAYLIST_DOWN));
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        //assumes all deactivated items will be at the bottom!
        DefaultListModel model = (DefaultListModel) getList().getModel();
        int index = getList().getSelectedIndex();

        if (index < model.getSize() - 1) {
            final SlimPlaylistItem song1 = (SlimPlaylistItem) model.getElementAt(index);
            final SlimPlaylistItem song2 = (SlimPlaylistItem) model.getElementAt(index + 1);

            try {
                getPlaylist().swap(song1, song2);
            } catch (SlimException ex) {
                Logger.getLogger(PlaylistUpAction.class.getName()).log(Level.SEVERE, null, ex);
            }

            Object temp = model.remove(index);
            model.add(index + 1, temp);
            getList().setSelectedValue(song1, true);
        }
    }
}
